package conc.testme;

import java.util.ArrayList;
import java.util.Date;

import android.content.Context;
import android.database.Cursor;

@SuppressWarnings("serial")
public class ListaEstatistica extends ArrayList<Estatistica> {

	DataBase db;

	public ListaEstatistica(Context context, DataBase db) {

		super();

		this.db = db;

	}

	public void novaConsulta(String tabelas, String[] colunas, String where,
			String[] valoresWhere, String having, String groupBy,
			String limite, String ordena) {

		this.clear();
		
		Cursor cursor = consulta(tabelas, colunas, where, valoresWhere,
				groupBy, having, ordena, limite);

		Estatistica estatistica;

		// indices dos campos da tabela questao
		int dataIndex = cursor.getColumnIndex(DataBase.ESTATISTICA.DATA
				.toString());
		int numeroQuestoesIndex = cursor
				.getColumnIndex(DataBase.ESTATISTICA.NUMEROQUESTOES.toString());
		int numeroAcertosIndex = cursor
				.getColumnIndex(DataBase.ESTATISTICA.PORCENTAGEMACERTOS
						.toString());

		

		while (!cursor.isAfterLast()) {
			estatistica = new Estatistica(
					new Data(cursor.getString(dataIndex)), new Long(
							cursor.getLong(numeroQuestoesIndex)), (new Long(
							cursor.getLong(numeroAcertosIndex))).intValue());

			this.add(estatistica);

			cursor.moveToNext();

		}

	}

	public Cursor consulta(String tabelas, String[] colunas, String where,
			String[] valoresWhere, String groupBy, String having,
			String orderBy, String limite) {
		return db.query(tabelas, colunas, where, valoresWhere, groupBy, having,
				orderBy, limite);
	}
	
	public long inserirEstatistica(float porcentagemAcertos, int numeroQuestoes) {
		Data dataAtual = new Data();
		dataAtual.setTime(new Date());
		return db.insertEstatistica(dataAtual , porcentagemAcertos, numeroQuestoes);
	}
}
